﻿using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScormModel;
using WPFFolderBrowser;
namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for NewCourse.xaml
    /// </summary>
    public partial class NewCourse : Window
    {
        TreeView tvMain;
        ImsManifest imsmanifest;
        RibbonBarRegion.RibbonBarHeader parentChild;
        public TreeView MainTree
        {
            get { return this.tvMain; }
            set { this.tvMain = value; }
        }
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public RibbonBarRegion.RibbonBarHeader ParentChild
        {
            get { return this.parentChild; }
            set { this.parentChild = value; }
        }
        public NewCourse()
        {
            InitializeComponent();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (useTemplate.IsChecked == true)
                {
                    string path = Directory.GetCurrentDirectory() + "\\Templates\\" + ((ComboBoxItem)templateComboBox.SelectedItem).Name + ".xml";
                    XmlDocument mainXmlDoc = new XmlDocument();
                    mainXmlDoc.Load(path);
                    //Get root element for the document
                    XmlNode rootElement = mainXmlDoc.DocumentElement;
                    imsmanifest.TemplateCourse = ((ComboBoxItem)templateComboBox.SelectedItem).Name;
                    imsmanifest = imsmanifest.buildFromXmlTemplate(rootElement);
                    imsmanifest.ManifestOrganizations.ListOrganization[0].Author = cAuthor.Text;
                    imsmanifest.ManifestOrganizations.ListOrganization[0].Title = cTitle.Text;
                    imsmanifest.ManifestOrganizations.ListOrganization[0].Description = cDes.Text;
                    imsmanifest.ManifestOrganizations.ListOrganization[0].Date = DateTime.Now.ToString();
                    imsmanifest.xmlNameSpace.Add("", "");
                    imsmanifest.xmlNameSpace.Add("", "http://www.imsglobal.org/xsd/imscp_v1p1");
                    imsmanifest.xmlNameSpace.Add("adlcp", "http://www.adlnet.org/xsd/adlcp_v1p3");
                    imsmanifest.xmlNameSpace.Add("adlseq", "http://www.adlnet.org/xsd/adlseq_v1p3");
                    imsmanifest.xmlNameSpace.Add("imsss", "http://www.imsglobal.org/xsd/imsss");
                    imsmanifest.xmlNameSpace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    imsmanifest.xmlNameSpace.Add("adlnav", "http://www.adlnet.org/xsd/adlnav_v1p3");
                    imsmanifest.SchemaLocation = "http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xsd http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd";
                    TreeViewItem root = imsmanifest.createManifestTreeViewItem();
                    ManifestProcessing mp = new ManifestProcessing(imsmanifest);
                    mp.BuildTitleHistory();
                    imsmanifest.clearResources();
                    tvMain.Items.Clear();
                    tvMain.Items.Add(root);
                }
                else
                {
                    imsmanifest.TemplateCourse = "Custom";
                    imsmanifest.Identifier = DateTime.Now.Ticks.ToString();
                    imsmanifest.Version = "1.3";
                    imsmanifest.ManifestMetadata = new Metadata();
                    imsmanifest.ManifestMetadata.Schema = "ADL SCORM";
                    imsmanifest.ManifestMetadata.SchemaVersion = "CAM 1.3";
                    imsmanifest.ManifestOrganizations = new Organizations();
                    imsmanifest.ManifestOrganizations.Default = "TOC";
                    Organization org = new Organization();
                    org.Type = "COURSE";
                    org.Identifier = "TOC";
                    org.Author = cAuthor.Text;
                    org.Title = cTitle.Text;
                    org.Description = cDes.Text;
                    org.Date = DateTime.Now.ToString();
                    org.ListItem = new List<Item>();
                    imsmanifest.ManifestOrganizations.ListOrganization = new List<Organization>();
                    imsmanifest.ManifestOrganizations.ListOrganization.Add(org);
                    imsmanifest.Resources = new ScormModel.Resources();
                    imsmanifest.Resources.ListResource = new List<Resource>();
                    imsmanifest.SequencingCollection = new SequencingCollection();
                    imsmanifest.SequencingCollection.ListSequencing = new List<Sequencing>();
                    imsmanifest.xmlNameSpace.Add("", "");
                    imsmanifest.xmlNameSpace.Add("", "http://www.imsglobal.org/xsd/imscp_v1p1");
                    imsmanifest.xmlNameSpace.Add("adlcp", "http://www.adlnet.org/xsd/adlcp_v1p3");
                    imsmanifest.xmlNameSpace.Add("adlseq", "http://www.adlnet.org/xsd/adlseq_v1p3");
                    imsmanifest.xmlNameSpace.Add("imsss", "http://www.imsglobal.org/xsd/imsss");
                    imsmanifest.xmlNameSpace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    imsmanifest.xmlNameSpace.Add("adlnav", "http://www.adlnet.org/xsd/adlnav_v1p3");
                    imsmanifest.SchemaLocation = "http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xsd http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd";
                    TreeViewItem root = imsmanifest.initManifest();
                    ManifestProcessing mp = new ManifestProcessing(imsmanifest);
                    mp.BuildTitleHistory();
                    tvMain.Items.Clear();
                    tvMain.Items.Add(root);
                }
                String coursePath = cLocation.Text + "\\" + cTitle.Text;
                parentChild.CurrentPath = coursePath.Replace("\\", "/");
                parentChild.CourseTitle = cTitle.Text;
                
                if (!Directory.Exists(coursePath))
                {
                    Directory.CreateDirectory(coursePath);
                }
                if (!Directory.Exists(coursePath + "\\Util"))
                {
                    Directory.CreateDirectory(coursePath + "\\Util");
                }
                if (!Directory.Exists(coursePath + "\\Images"))
                {
                    Directory.CreateDirectory(coursePath + "\\Images");
                }
                if (!Directory.Exists(coursePath + "\\css"))
                {
                    Directory.CreateDirectory(coursePath + "\\css");
                }
                if (!Directory.Exists(coursePath + "\\js"))
                {
                    Directory.CreateDirectory(coursePath + "\\js");
                }
                if (!Directory.Exists(coursePath + "\\media"))
                {
                    Directory.CreateDirectory(coursePath + "\\media");
                }
                string schemaPath = Directory.GetCurrentDirectory() + "\\Schema";
                string utilPath = Directory.GetCurrentDirectory() + "\\Util";
                FileUtil.Copy(schemaPath, coursePath);
                FileUtil.Copy(utilPath, coursePath + "\\Util");
                FileUtil.Copy(Directory.GetCurrentDirectory() + "\\Images", coursePath + "\\Images");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Create course fail");
            }

        }
        private void LoadTree()
        {
            TreeViewItem treeItem = null;

            // North America 
            treeItem = GetTreeView("North America", Colors.Green);

            treeItem.Items.Add(GetTreeView("USA", "usa.png"));
            treeItem.Items.Add(GetTreeView("Canada", "canada.png"));
            treeItem.Items.Add(GetTreeView("Mexico", "mexico.png"));

            tvMain.Items.Add(treeItem);

            // South America 
            treeItem = GetTreeView("South America", Colors.LightGreen);
            TreeViewItem item1 = GetTreeView("Argentina", "argentina.png");
            treeItem.Items.Add(GetTreeView("Argentina", "argentina.png"));
            treeItem.Items.Add(GetTreeView("Brazil", "brazil.png"));
            treeItem.Items.Add(GetTreeView("Uruguay", "uruguay.png"));
            item1.Items.Add(treeItem);
            tvMain.Items.Add(item1);

            // Europe
            treeItem = GetTreeView("Europe", Colors.Brown);

            treeItem.Items.Add(GetTreeView("UK", "uk.png"));
            treeItem.Items.Add(GetTreeView("Denmark", "denmark.png"));
            treeItem.Items.Add(GetTreeView("France", "france.png"));

            tvMain.Items.Add(treeItem);

            // Asia
            treeItem = GetTreeView("Asia", Colors.Red);

            treeItem.Items.Add(GetTreeView("Pakistan", "pakistan.png"));
            treeItem.Items.Add(GetTreeView("Japan", "japan.png"));
            treeItem.Items.Add(GetTreeView("China", "china.png"));

            tvMain.Items.Add(treeItem);

            // Asia
            treeItem = GetTreeView("Africa", Colors.Yellow);

            treeItem.Items.Add(GetTreeView("Somalia", "Somalia.png"));
            treeItem.Items.Add(GetTreeView("Uganda", "Uganda.png"));
            treeItem.Items.Add(GetTreeView("Egypt", "Egypt.png"));

            tvMain.Items.Add(treeItem);


            // Australia
            treeItem = GetTreeView("Australia", Colors.Purple);

            treeItem.Items.Add(GetTreeView("Australia", "australia.gif"));
            tvMain.Items.Add(treeItem);


            // Antarctica
            treeItem = GetTreeView("Antarctica", Colors.Blue);

            tvMain.Items.Add(treeItem);

        }

        private TreeViewItem GetTreeView(string text, string imagePath)
        {
            TreeViewItem item = new TreeViewItem();

            item.IsExpanded = true;

            // create stack panel
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;

            // create Image
            Image image = new Image();
            image.Source = new BitmapImage(new Uri("pack://application:,,/Images/" + imagePath));

            // Label
            Label lbl = new Label();
            lbl.Content = text;


            // Add into stack
            stack.Children.Add(image);
            stack.Children.Add(lbl);

            // assign stack to header
            item.Header = stack;
            return item;

        }

        private TreeViewItem GetTreeView(string text, Color boxColor)
        {
            TreeViewItem item = new TreeViewItem();
            item.IsExpanded = true;

            // create stack panel
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;



            // create Image

            Border border = new Border();
            border.Width = 8;
            border.Height = 12;
            border.Background = new SolidColorBrush(boxColor);





            // Label
            Label lbl = new Label();
            lbl.Content = text;


            stack.Children.Add(border);
            stack.Children.Add(lbl);

            //item.HeaderTemplate.ad  


            item.Header = stack;
            return item;

        }

        private void BrowseFolder_Click(object sender, RoutedEventArgs e)
        {
            WPFFolderBrowserDialog dlg = new WPFFolderBrowserDialog();
            if (dlg.ShowDialog() == true)
            {
                cLocation.Text = dlg.FileName;

            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            templateComboBox.IsEnabled = true;
            btnReview.IsEnabled = true;
        }

        private void CheckBox_UnChecked(object sender, RoutedEventArgs e)
        {
            templateComboBox.IsEnabled = false;
            btnReview.IsEnabled = false;
        }
    }
}
